package com.varian.boot.listener;

import cn.hutool.core.util.StrUtil;
import cn.hutool.extra.spring.SpringUtil;
import com.varian.boot.constant.BootConstant;
import com.varian.misc.ApplicationStartedCustomizer;
import com.varian.tool.Func;
import com.varian.tool.enums.EnvironmentProfileEnum;
import org.springframework.boot.availability.AvailabilityChangeEvent;
import org.springframework.boot.availability.ReadinessState;
import org.springframework.core.Ordered;

/**
 * @author ben
 * @since 2024/6/22
 */
public class DefaultApplicationStarted implements ApplicationStartedCustomizer, Ordered {
    @Override
    public String afterMessage(AvailabilityChangeEvent<ReadinessState> event) {
        String applicationName = SpringUtil.getApplicationName();
        EnvironmentProfileEnum activeProfile = Func.getProfile();
        String port = SpringUtil.getApplicationContext().getEnvironment().getProperty("server.port");
        return StrUtil.format("[{}] profile '{}' started on port {}", applicationName, activeProfile.getProfile(), port);
    }

    @Override
    public int getOrder() {
        return BootConstant.DEFAULT_STARTED_ORDER;
    }
}
